using SinceUnity.LobbyNetwork;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;

public enum HitEffect
{
    Freeze,
    Boom,
}
public class Battle : MonoBehaviour
{
	public HashSet<ulong> readyClients = new HashSet<ulong>();
	public GameObject Obj_Mask;
	public static Battle Instance;
	public float Edge = 10f;

    [HideInInspector] public BattleLevel NowLevel;


	public HitEffect Effect;

	void Awake()
	{
		Instance = this;


		// 等待 所有玩家摆渡就绪
        LobbyFerry.NOW.WaitAllFerryEnd(() =>
		{
            StartGame();

        }).ClientReadyServerRpc();
	}



	async void StartGame()
	{
		Obj_Mask.SetActive(false);

		var handle = Addressables.LoadAssetAsync<GameObject>("BattleLevel");
		await handle.Task;
		var go = Instantiate(handle.Result);
		NowLevel = go.GetComponent<BattleLevel>();
	}

	void Update()
	{
		if (Input.GetMouseButtonDown(0))
		{
			Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

			// 计算方形区域的边界
			float minX = mousePosition.x - Edge / 2;
			float maxX = mousePosition.x + Edge / 2;
			float minY = mousePosition.y - Edge / 2;
			float maxY = mousePosition.y + Edge / 2;

			// 获取场景中所有Enemy
			Enemy[] enemies = FindObjectsOfType<Enemy>();

			// 检查每个Enemy是否在方形区域内
			foreach (Enemy enemy in enemies)
			{
				if (!enemy.IsOwner) continue;

				Vector2 pos = enemy.transform.position;

				// 判断xy坐标是否在方形区域内
				if (pos.x >= minX && pos.x <= maxX &&
					pos.y >= minY && pos.y <= maxY)
				{
					EnableEffect(enemy, mousePosition);
				}
			}
		}
	}

	void EnableEffect(Enemy enemy, Vector3 boomPosition)
	{



        if (Effect == HitEffect.Freeze)
			enemy.gameObject.AddComponent<Freeze>();
		else if (Effect == HitEffect.Boom)
		{
			enemy.gameObject.AddComponent<Boom>().BoomPosition = boomPosition;
		}
	}
}
